/**
 * 
 */
package com.vision.core.cm.db.data;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Table;

import net.sf.gilead.pojo.gwt.LightEntity;

import com.vision.core.cm.data.ApplicationId;
import com.vision.core.cm.data.Domain;

/**
 * @author Mark
 *
 */
@Entity
@Table(name = "permission")
public class Permission extends LightEntity implements Serializable {
	
	public static interface FieldSpecs {
		int NAME_MAX = 100;
		int DESC_MAX = 300;
	}

	private static final long serialVersionUID = -1240814969994131740L;
	
	public static final String COL_NAME = "name";
	public static final String COL_DESCRIPTION = "description";
	public static final String COL_APP_ID = "app_id";
	public static final String COL_DOMAIN = "domain";

	@Id
	@Column(name = COL_NAME, updatable = false, length = FieldSpecs.NAME_MAX)
	private String name;
	
	@Column(name = COL_DESCRIPTION, length = FieldSpecs.DESC_MAX)
	private String description;
	
	@Column(name = COL_APP_ID, nullable = false, updatable = false)
	@Enumerated(EnumType.STRING)
	private ApplicationId applicationId;
	
	@Column(name = COL_DOMAIN, nullable = false, updatable = false)
	@Enumerated(EnumType.STRING)
	private Domain domain;
	
	public Permission() { }
	
	public Permission(ApplicationId applicationId, Domain domain, String name, String description) {
		this.applicationId = applicationId;
		this.domain = domain;
		this.name = name;
		this.description = description;
	}
	
	@Override
	public boolean equals(Object arg0) {
		if (arg0 instanceof Permission) {
			Permission p = (Permission) arg0;
			return getName().equals(p.getName());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return getName() == null ? super.hashCode() : getName().hashCode();
	}
	
	@Override
	public String toString() {
		return getApplicationId() + "-" + getName();
	}
	
	public ApplicationId getApplicationId() {
		return applicationId;
	}

	public void setApplicationId(ApplicationId applicationId) {
		this.applicationId = applicationId;
	}

	public Domain getDomain() {
		return domain;
	}

	public void setDomain(Domain domain) {
		this.domain = domain;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}
